package zdc.display {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFormat;

import org.tuio.TuioTouchEvent;

import zdc.ui.MovingInertia;
import zdc.Util;

/**
 * 序列帧影片剪辑
 * @author Simple Slime
 */
public class SeqMovieClip extends MovieClip {
    protected var target:MovieClip = null;
    protected var mi:MovingInertia;
    protected var recur:Boolean = true;
    protected var autoSetup:Boolean = true;
    protected var preview:DisplayObject;
    protected var gesture:DisplayObject;
    protected var gestX:Number, gestY:Number;
    protected var prevX:Number, prevY:Number;
    protected var walkDistX:Number = 5;
    protected var bpds:Vector.<BitmapData> = new Vector.<BitmapData>();
    protected var bmp:Bitmap;
    protected var curIdx:int = 1;
    protected var preLoadNum:int = 2;
    protected var lastMc:MovieClip;

    /**
     *
     * @param    wrapper
     * @param    preview
     * @param    gesture
     * @param    rec
     */
    public function SeqMovieClip(wrapper:MovieClip = null, preview:DisplayObject = null, gesture:DisplayObject = null, rec:Boolean = true, autoSetup:Boolean = true) {
        target = wrapper ? wrapper : this;
        recur = rec;
        this.autoSetup = autoSetup;

        this.preview = preview;
        this.gesture = gesture;
        if (gesture) {
            addChild(gesture);
            gesture.visible = false;
            gestX = gesture.x;
            gestY = gesture.y;
        }
        if (preview) {
            addChild(preview);
            prevX = preview.x;
            prevY = preview.y;
        }

        initPreVars();
        target.stage ? init() : target.addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void {
        target.removeEventListener(Event.ADDED_TO_STAGE, init);
        initPostVars();
        initStates();
        initEvents();
        run();

    }

    private function initPreVars():void {

    }

    private function initPostVars():void {

    }

    private function initStates():void {
    }

    /**
     * 设置动作
     */
    public function setupActor():void {			//trace("SeqMovieClip setupActor");
        target.gotoAndStop(1);
        mi = new MovingInertia(this, null, TuioTouchEvent.TOUCH_DOWN, TuioTouchEvent.TOUCH_UP, TuioTouchEvent.TOUCH_MOVE, TuioTouchEvent.ROLL_OUT);
        //mi.addMoveHook(onMove);
        mi.addWalkHook(onMove);
        mi.minWalkDistX = walkDistX;
        mi.WALK_TYPE = true;
        mi.enable = true;

        removeChild(preview);
        gesture.visible = true;
        Util.top(gesture);
    }

    private function onMove(e:Event, cdx:Number, cdy:Number):void {
        if (cdx < 0) {
            if (target.currentFrame == 1) {
                target.gotoAndStop(target.totalFrames);
            }
            else {
                target.prevFrame();
            }
        }
        else if (cdx > 0) {
            if (target.currentFrame == target.totalFrames) {
                target.gotoAndStop(1);
            }
            else {
                target.nextFrame();
            }
        }
    }


    private function initEvents():void {
        target.addEventListener(Event.REMOVED_FROM_STAGE, onRemoved);
    }

    private function onRemoved(e:Event):void {
        target.removeEventListener(Event.REMOVED_FROM_STAGE, onRemoved);

    }

    private function run():void {
        if (autoSetup) {
            setupActor();
        }
    }

    private function showMc(mc:MovieClip):void {
        //mc.visible = true;
        mc.x = mc.y = 0;
        mc.alpha = 1;
    }

    private function hideAll():void {
    }

    /**
     * 手势显示对象的X坐标
     */
    public function get gestureX():Number {
        return gestX;
    }

    public function set gestureX(value:Number):void {
        gestX = value;
        gesture.x = gestX;
    }

    /**
     * 手势显示对象的Y坐标
     */
    public function get gestureY():Number {
        return gestY;
    }

    public function set gestureY(value:Number):void {
        gestY = value;
        gesture.y = gestY;
    }

    /**
     * 预览显示对象的X坐标
     */
    public function get previewX():Number {
        return prevX;
    }

    public function set previewX(value:Number):void {

        prevX = value;
        preview.x = prevX;
    }

    /**
     * ...0
     * 预览显示对象的Y坐标
     */
    public function get previewY():Number {
        return prevY;
    }

    public function set previewY(value:Number):void {
        prevX = value;
        preview.y = prevY;
    }
}

}